package com.longxi.tianjintianbao.ui.presenter.equipment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.SearchListener;
import com.longxi.tianjintianbao.listener.SelectEquipmentListener;
import com.longxi.tianjintianbao.model.Search;
import com.longxi.tianjintianbao.model.equipment.Equipment;
import com.longxi.tianjintianbao.model.equipment.EquipmentInspection;
import com.longxi.tianjintianbao.model.equipment.EquipmentType;
import com.longxi.tianjintianbao.ui.activity.equipment.EquipmentListDetailsActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.LabelViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.SearchViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.equipment.EquipmentInspectionViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.equipment.EquipmentViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.equipment.IEquipmentAtView;
import com.longxi.tianjintianbao.ui.view.equipment.IEquipmentInspectionAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class EquipmentInspectionAtPresenter extends BasePresenter<IEquipmentInspectionAtView> {

    public static String EQUIPMENTID = "EQUIPMENTID";//单据Activity之间传值key
    private MultiTypeAdapter adapter;
    private Items items;
    String equipmentid = "";

    public EquipmentInspectionAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            SearchListener searchListener = new SearchListener() {
                @Override
                public void onSearch(String keyword) {

                }
            };
            adapter.register(Search.class, new SearchViewBinder(searchListener));
            adapter.register(EquipmentInspection.class, new EquipmentInspectionViewBinder());
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setBar() {
        mContext.setTitle("巡检记录");
        mContext.setTvLeft(MyUtils.getString(R.string.home));
        equipmentid = mContext.getIntent().getStringExtra(EQUIPMENTID);
    }

    /**
     * 测试数据
     */
    public void getEquipmentInspection(int page, int limit){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("equipmentid", equipmentid);
            object.put("page", page);
            object.put("rowcount", limit);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getEquipmentInspection(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<EquipmentInspection>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                MyUtils.showToast(throwable.message);
                mContext.showError();
            }

            @Override
            public void onNext(HttpResult<EquipmentInspection> result) {
                items = NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mContext);
            }
        });
    }

    public void getDate(){
        items = new Items();
        for (int i = 0; i < 50; i++) {
            items.add(new EquipmentInspection("1",
                    "巡检单号" + i,
                    "巡检开始日期" + i,
                    "巡检开始日期" + i,
                    "你懂的233333333333333333333333333333333333333333333333333333333333333333" + i,
                    "里还浪" + i,
                    "状态" + i));
        }
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
        mContext.hideLoading();
    }

}
